<template>
  <el-dialog v-if="load" :close-on-click-modal="false" title="预估销量系数设置" v-model="visible" append-to-body width="660px" :z-index="2000" @close="handleClose">
    <el-form label-position="top">
      <div class="grid grid-cols-2 gap-x-3">
        <el-form-item label="周上升且月上升">
          <el-input-number v-model="form.data[1]" controls-position="right" :min="0" :step="1" :precision="2"
            class="w-full" placeholder="请填写系数" />
        </el-form-item>
        <el-form-item label="周下降且月上升">
          <el-input-number v-model="form.data[2]" controls-position="right" :min="0" :step="1" :precision="2"
            class="w-full" placeholder="请填写系数" />
        </el-form-item>
        <el-form-item label="周上升且月下降">
          <el-input-number v-model="form.data[3]" controls-position="right" :min="0" :step="1" :precision="2"
            class="w-full" placeholder="请填写系数" />
        </el-form-item>
        <el-form-item label="周下降且月下降">
          <el-input-number v-model="form.data[4]" controls-position="right" :min="0" :step="1" :precision="2"
            class="w-full" placeholder="请填写系数" />
        </el-form-item>
        <el-form-item label="周持平或月持平">
          <el-input-number v-model="form.data[5]" controls-position="right" :min="0" :step="1" :precision="2"
            class="w-full" placeholder="请填写系数" />
        </el-form-item>
      </div>
      <el-alert title="说明" type="info" show-icon :closable="false">
        系统预估销量=销售系数*加权日平均销量，各销售系数为空时，默认为1。
      </el-alert>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
const api = inject('api')
const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({ data: {} })
const type = inject('type')

const emit = defineEmits(['close', 'success'])

// 打开弹窗
const open = async (selection) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  const is_ids = selection.map(d => d.is_id)
  const data = {
    1: null,
    2: null,
    3: null,
    4: null,
    5: null,
  }
  if (selection.length === 1) {
    const item = selection[0].estimate_sale_ratio
    for (const key in item) {
      data[key] = item[key]
    }
  }
  form.value = { is_ids, data, type }
  loadings = reactive({})
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = () => {
  loadings.submit = true
  api.saleRatio(form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>